package main.Q2201_2300;

public class Q2261_2270 {
    public static void main(String[] args) {
        System.out.println("Question2261：");
        System.out.println("Question2262：");
        System.out.println("Question2263：");
        System.out.println("Question2264：");
        System.out.println("Question2265：统计值等于子树平均值的节点数");
        System.out.println("Question2266：");
        System.out.println("Question2267：");
        System.out.println("Question2268：");
        System.out.println("Question2269：");
        System.out.println("Question2270：");
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) {
        this.val = val;
    }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Question2265 {
    int result = 0;
    class Info {
        int total, count;
        public Info(int total, int count) {
            this.count = count;
            this.total = total;
        }
    }
    public Info process(TreeNode root) {
        if (root == null) {
            return new Info(0, 0);
        }
        Info leftInfo = process(root.left);
        Info rightInfo = process(root.right);
        int newCount = leftInfo.count + rightInfo.count + 1;
        int newTotal = leftInfo.total + rightInfo.total + root.val;
        if (newTotal / newCount == root.val) result++;
        return new Info(newTotal, newCount);
    }
    public int averageOfSubtree(TreeNode root) {
        process(root);
        return result;
    }
}